package com.qingxuan.mybatis.plus.study.demo1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingxuan.mybatis.plus.study.demo1.entity.DocNewsMain;
import com.qingxuan.mybatis.plus.study.demo1.entity.SysOrgDept;
import com.qingxuan.mybatis.plus.study.demo1.entity.SysUser;
import com.qingxuan.mybatis.plus.study.demo1.service.impl.SysOrgDeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author yy.lei
 * @since 2021-10-16
 */
@Slf4j
@Controller
@RequestMapping("/sysOrgDept")
public class SysOrgDeptController {

	@Autowired
	private SysOrgDeptServiceImpl sysOrgDeptService;

	@ResponseBody
	@PostMapping("/add")
	public boolean add(@RequestBody SysOrgDept sysOrgDept) {
		log.info("[保存] 用户保存的方法被调用，sysOrgDept：{}",sysOrgDept.toString());
		return sysOrgDeptService.save(sysOrgDept);
	}

	@ResponseBody
	@PostMapping("/update")
	public boolean update(@RequestBody SysOrgDept sysOrgDept) {
		log.info("[部门更新] 用户更新的方法被调用，sysUser：{}",sysOrgDept.toString());
		return sysOrgDeptService.updateById(sysOrgDept);
	}

	@ResponseBody
	@GetMapping("/get")
	public SysOrgDept get(@RequestParam Long id) {
		log.info("[获取单个部门对象] 获取对象的方法被调用，id：{}",id);
		return sysOrgDeptService.getById(id);
	}

	@ResponseBody
	@PostMapping("/remove")
	public boolean remove(@RequestParam Long id) {
		log.info("[删除单个部门对象] 删除方法被调用，id：{}",id);
		return sysOrgDeptService.removeById(id);
	}

	@ResponseBody
	@GetMapping("/page")
	public IPage<SysOrgDept> page(@RequestParam(value = "page_size") Integer pageSize,
		@RequestParam(value = "page_num") Integer pageNum) {
		log.info("[分页查询] 分页查询方法被调用，pageSize：{},pageNum：{}",pageSize,pageNum);
		Page<SysOrgDept> page = new Page<>(pageSize, pageNum);
		return sysOrgDeptService.page(page);
	}

	@ResponseBody
	@GetMapping("/pageWithWrapper")
	public IPage<SysOrgDept> pageWithWrapper(@RequestParam(value = "page_size") Integer pageSize,
		@RequestParam(value = "page_num") Integer pageNum,
		@RequestParam(value = "name",required = false)String name) {
		log.info("[条件分页查询] 分页查询方法被调用，pageSize：{},pageNum：{},name：{}",pageSize,pageNum,name);
		QueryWrapper<SysOrgDept> queryWrapper =  new QueryWrapper<>();
		queryWrapper.like("name",name);
		queryWrapper.orderByDesc("update_time");
		Page<SysOrgDept> page = new Page<>(pageSize, pageNum);
		return sysOrgDeptService.page(page,queryWrapper);
	}

}
